package com.ruoyi.web.controller.kaoyan;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kaoyan.domain.EducationBanner;
import com.ruoyi.kaoyan.service.EducationBannerService;
import com.ruoyi.system.domain.SysFileInfo;
import com.ruoyi.system.service.ISysFileInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 后台考研陪伴平台首页轮播图后台管理
 * @author admin
 */
@Api(tags = "后台轮播管理", value = "后台轮播管理")
@RestController
@RequestMapping("/education/banner")
public class EducationBannerController extends BaseController {

    @Autowired
    private EducationBannerService bannerService;
    @Autowired
    private ISysFileInfoService fileInfoService;

    @ApiOperation("banner列表")
    @PostMapping("/list")
    public AjaxResult list(String title){
        LambdaQueryWrapper<EducationBanner> queryWrapper = new LambdaQueryWrapper<EducationBanner>()
                .like(StringUtils.isNotBlank(title), EducationBanner::getTitle, title);
        Page<EducationBanner> bannerPage = bannerService.page(startPagePlus(), queryWrapper);
        List<EducationBanner> bannerList = bannerPage.getRecords();
        List<String> groupIdSet = bannerList.stream().map(EducationBanner::getBannerGroupId).collect(Collectors.toList());
        Map<String, String> bannerPathMap = fileInfoService.getFilePathMap(groupIdSet);
        bannerList.forEach(e -> e.setBannerUrl(bannerPathMap.get(e.getBannerGroupId())));
        return AjaxResult.ok().page(bannerPage, bannerList).build();
    }

    @ApiOperation("banner添加或编辑")
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody EducationBanner banner){
        if (StringUtils.checkObjAllFieldsIsNull(banner)){
            return AjaxResult.error("不能为空");
        }
        boolean update = bannerService.saveOrUpdate(banner);
        return toAjax(update);
    }

    @ApiOperation("删除banner")
    @PostMapping("/remove")
    public AjaxResult removeById(Long id){
        boolean remove = bannerService.removeById(id);
        return toAjax(remove);
    }
}
